if [ "$#" -ne 2 ]; then
    echo Enter the path to the directory to convert and path to har.
    exit 2
fi

nodePath=$(realpath $(dirname $0)/node)
basePath=$(realpath $1)
harPath=$(realpath $2)
echo The root folder for arkoala adding: $basePath
echo The har path: $harPath

cd $nodePath
npm i

installFolder() {
    folderPath=$(realpath $1)
    echo *********************************************************
    echo folderPath: $folderPath
    echo nodePath: $nodePath
    echo *******************ohpm install**************************
    cd $folderPath
    ohpm i $harPath --strict_ssl false
    echo *******************ohpm install end**********************
    cd $nodePath
    if [ -d "$folderPath/entry/src/main/ets/test" ]; then
        echo OK: main module is found in $folderPath
        npm run arkoala $folderPath/entry/src/main/module.json5
    elif [ -d "$folderPath/entry/src/ohosTest/ets/test" ]; then
        echo OK: ohosTest module is found in $folderPath
        npm run arkoala $folderPath/entry/src/ohosTest/module.json5
    else
        echo WARNING: non-standard directory structure
        find $folderPath -path "*/src/*" -name "module.json5" -exec bash -c 'npm run arkoala {}' \;
    fi
    echo *********************************************************
}

find $basePath -type d -name "hvigor" | while read file; do installFolder "$file/.."; done
